package com.qiall.project.service;

import com.qiall.assertutil.Assert;
import com.qiall.common.entity.PageQuery;
import com.qiall.common.entity.PageResponse;
import com.qiall.common.exception.BusinessResult;
import com.qiall.common.request.BaseRequest;
import com.qiall.common.request.BaseResponse;
import com.qiall.project.ability.ProjectService;
import com.qiall.project.ability.request.AddProjectRequest;
import com.qiall.project.ability.request.GetProjectByPageRequest;
import com.qiall.project.ability.request.GetProjectRequest;
import com.qiall.project.ability.request.ProjectResponse;
import com.qiall.project.data.entity.ProjectInfo;
import com.qiall.project.data.mapper.ProjectInfoMapper;
import com.qiall.project.service.addproject.AddProjectCheckActivity;
import com.qiall.project.service.addproject.ProjectTrans;
import com.qiall.user.ability.entity.UserInfoDTO;
import com.qiall.user.ability.service.user.UserService;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.compress.utils.Lists;
import org.apache.dubbo.config.annotation.DubboReference;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.List;
import java.util.stream.Collectors;

/**
 * @author 吴金辉
 */
@DubboService
@Component
public class ProjectServiceImpl implements ProjectService {

    @Autowired
    private ProjectInfoMapper projectInfoMapper;

    @Autowired
    private AddProjectCheckActivity addProjectCheckActivity;

    @Autowired
    private ProjectTrans projectTrans;

    @DubboReference
    private UserService userService;

    @Override
    public BusinessResult<BaseResponse> addOrUpdateProject(AddProjectRequest request) {
        BaseResponse baseResponse = new BaseResponse();
        addProjectCheckActivity.handler(request, baseResponse);
        ProjectInfo projectInfo = projectTrans.trans(request);
        if (request.getId() == null) {
            projectInfoMapper.insert(projectInfo);

        } else {
            projectInfoMapper.update(projectInfo);
        }
        return BusinessResult.success(baseResponse);
    }

    @Override
    public BusinessResult<ProjectResponse> getProject(GetProjectRequest getProjectRequest) {
        ProjectInfo projectInfo = projectInfoMapper.select(getProjectRequest.getProjectId());
        ProjectResponse projectResponse = projectTrans.trans(projectInfo);
        return BusinessResult.success(projectResponse);
    }

    @Override
    public BusinessResult<PageResponse<ProjectResponse>> getProjectByPage(GetProjectByPageRequest getProjectByPageRequest) {
        ProjectInfo queryBean = new ProjectInfo();
        queryBean.setProjectName(getProjectByPageRequest.getProjectName());
        // 设置
        BaseRequest baseRequest=new BaseRequest();
        baseRequest.setAccessToken(getProjectByPageRequest.getAccessToken());
        BusinessResult<UserInfoDTO> userInfoDTOBusinessResult = userService.getUserInfoByAuthCode(baseRequest);
        Assert.notNull(userInfoDTOBusinessResult.getBody(), "用户未登录");
        Integer orgId = userInfoDTOBusinessResult.getBody().getOrgId();
        queryBean.setOrgId(orgId);
        PageQuery pageQuery = getProjectByPageRequest.getPageQuery();
        int count = projectInfoMapper.count(queryBean, pageQuery.getStartTime(), pageQuery.getEndTime());
        if (count == 0) {
            return BusinessResult.success(PageResponse.nullValue(pageQuery));
        }
        List<ProjectInfo> projectInfoList = projectInfoMapper.selectByPage(queryBean, pageQuery.getStartTime(), pageQuery.getEndTime(), pageQuery.limit(), pageQuery.offset());
        List<ProjectResponse> projectResponses = projectInfoList.stream().map(x -> projectTrans.trans(x)).collect(Collectors.toList());
        PageResponse pageResponse = PageResponse.withValue(pageQuery, count, projectResponses);
        return BusinessResult.success(pageResponse);
    }

    @Override
    public BusinessResult<List<ProjectResponse>> getCurrentOrgProject(BaseRequest baseRequest) {
        BusinessResult<UserInfoDTO> userInfoDTOBusinessResult = userService.getUserInfoByAuthCode(baseRequest);
        Assert.notNull(userInfoDTOBusinessResult.getBody(), "用户未登录");
        Integer orgId = userInfoDTOBusinessResult.getBody().getOrgId();
        List<ProjectInfo> projectInfos = projectInfoMapper.selectByOrgId(orgId);
        if (CollectionUtils.isEmpty(projectInfos)) {
            return BusinessResult.success(Lists.newArrayList());
        }
        List<ProjectResponse> projectResponses = projectInfos.stream().map(x -> projectTrans.trans(x)).collect(Collectors.toList());
        return BusinessResult.success(projectResponses);
    }
}
